/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.server.dac.dao;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import pos.client.dac.dao.PianoDAO;
import pos.domain.Piano;
import pos.domain.Utente;
import pos.server.dac.ConnectionManager;
import pos.server.dac.exceptions.EccezioneCRUD;

/**
 *
 * @author Alessio
 */
public class PianoDAOImpl extends RemoteServiceServlet implements PianoDAO
{
    private String SELECT_SQL = "SELECT nome FROM piano WHERE id_piano = ?";
    private String INSERT_SQL = "INSERT INTO piano (nome) VALUES (?) RETURNING id_piano";
    private String INSERT_SQL2 = "INSERT INTO U_P (Utente, Piano) VALUES (?,?)";
    private String UPDATE_SQL = "UPDATE piano SET nome = ? WHERE id_piano = ?";
    private String DELETE_SQL = "DELETE FROM piano WHERE id_piano = ?";
    
    @Override
    public void leggiDatidaDB(Piano obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(SELECT_SQL);
            pstmt.setInt(1, obj.getId_piano());
            ResultSet result = pstmt.executeQuery();
            if(result.next()){
                obj.setNome(result.getString("nome"));
            }
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public Piano inserisciDatisuDB(Piano obj, Utente u) throws EccezioneCRUD
    {
        System.out.println("\nIn inserisciDatiSuDB");
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_SQL);
            pstmt.setString(1, obj.getNome());
            con.setAutoCommit(false);
            
            //pstmt.executeUpdate();
            //PreparedStatement st2 = con.prepareStatement("SELECT currval(piano_id_piano_seq)");
            ResultSet rs = pstmt.executeQuery();
            rs.next();
            obj.setId_piano(rs.getInt(1));
            
            pstmt = con.prepareStatement(INSERT_SQL2);
            pstmt.setString(1, u.getLogin());
            pstmt.setInt(2, obj.getId_piano());
            pstmt.executeUpdate();
            con.commit();
            con.setAutoCommit(true);
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        } finally{
            if(con != null){
                try {
                    con.setAutoCommit(true);
                    if(pstmt != null)
                        pstmt.close();
                    con.close();
                } catch (SQLException ex) {
                    throw new EccezioneCRUD(ex.getMessage());
                }
            }
        }
        return obj;
    }

    @Override
    public void aggiornaDatisuDB(Piano obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_SQL);
            pstmt.setInt(2, obj.getId_piano());
            pstmt.setString(1, obj.getNome());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }

    @Override
    public void cancellaDatidaDB(Piano obj) throws EccezioneCRUD
    {
        try {
            Connection con = null;
            PreparedStatement pstmt = null;
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_SQL);
            pstmt.setInt(1, obj.getId_piano());
            pstmt.executeUpdate();
            pstmt.close();
            con.close(); //si assume la modalita’ autocommit
        } catch (SQLException ex) {
            throw new EccezioneCRUD(ex.getMessage());
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex.getCause());
        }
    }
}
